/*
 *  Arrow
 *
 *  Created by David Giovannini on 1/5/07.
 *  Copyright 2007 Software by Jove. All rights reserved.
 *
 */
 
#include "SBJ/Arrow/Channel.h"
#include "MockMessage.h"

#include <boost/test/auto_unit_test.hpp>

BOOST_AUTO_TEST_CASE(Accume_All)
{
	::printf("Accume_All\n");
	// Create the channel
	SBJ::Arrow::Channel channel;

	// Publish our message type
	SBJ::Arrow::Publication& pub = channel.publish< MockMessage >();

	// TODO: create a hit for every message
	volatile const char* hit = 0;
	MockMessage::Accume accume;
	accume.upper = 999;
	accume.lower = 999;
	accume.acquired = 0;

	// Create the messages
	// They will all accume before dequeuing
	for ( int i = 0; i < accume.upper + 1; ++i )
	{
		MockMessage msg( hit, pub.getTopic< MockMessage >(), 0, accume );
		// Push the message
		channel.onMessage( msg );
	}

	// Start dequeuing
	channel.start();

	// And test
	::test_sleep( .5 );
	BOOST_CHECK_EQUAL( MockMessage::hit_execute, const_cast<char*>(hit) );
}

BOOST_AUTO_TEST_CASE(Accume_Some)
{
	::printf("Accume_Some\n");
	// Create the channel
	SBJ::Arrow::Channel channel;

	// Publish our message type
	SBJ::Arrow::Publication& pub = channel.publish< MockMessage >();

	// TODO: create a hit for every message
	volatile const char* hit = 0;
	MockMessage::Accume accume;
	accume.upper = 999;
	accume.lower = 0;
	accume.acquired = 0;

	// Creat the messages
	// They will all accume before dequeuing
	for ( int i = 0; i < accume.upper + 1; ++i )
	{
		MockMessage msg( hit, pub.getTopic< MockMessage >(), 0, accume );
		// Push the message
		channel.onMessage( msg );
		// Start dequeuing
		if ( (i+1) == (accume.upper+1)/2 ) channel.start();
	}

	// And test
	::test_sleep( .5 );
	BOOST_CHECK_EQUAL( MockMessage::hit_execute, const_cast<char*>(hit) );
}